#!/bin/sh
#-------------------------------------------------------------------------------
# restoreUpgradeFiles.sh
# 
# This shell script is invoked to perform a restore of previously saved upgrade
# data files.  It will be executed every time the system reboots.  If no
# "indicator" file is present, the script exits and no file restoration is done.
# If the indicator file is detected, a Java environment is established, and the
# actual class file to process the prior saved data is executed.
# 
# Usage: restoreUpgradeFiles
#
# Return Codes:
# 1 - Error accessing required save update data logging directory
# 2 - Media is write-protected
# 4 - Error mounting the media
# 6 - Error unmounting the media
# 7 - Generic other error(?)

#---------------------------------------------------------------------------------
# directory and filename which records the save upgrade data actions.
#---------------------------------------------------------------------------------
LOGDIR=/var/hsc/log
LOG=$LOGDIR/restoreUpgradeFiles.log

LOG_ERROR_LOG=/tmp/restoreUpgradeFiles.log

# STDIO directed to /tmp/mount_output + PID
MOUNT_OUTPUT=/tmp/mount_output$$

      
# common exit point for script
exit_cleanup() {
    rm -f $MOUNT_OUTPUT
    exit $1
}  


#-------------------------------------------------------------------------------
# the (hard drive) media mount point to which the save upgrade data is to be
# stored.  There should be an entry for this mountpoint in /etc/fstab
#-------------------------------------------------------------------------------
UPGRADE_MOUNTPOINT=/mnt/upgrade

#-------------------------------------------------------------------------------
# the restore indicator file
#-------------------------------------------------------------------------------
INDICATOR_FILE=$UPGRADE_MOUNTPOINT/doRestore

#-------------------------------------------------------------------------------
# the mount point for the media to which the save upgrade data is to be stored.
# A corresponding device entry should exist for this mount point in /etc/fstab
# Note: even though we're using the DVD, the 'auto' option in /etc/fstab will
#       figure out that it's not a CDROM device, but actually a DVD
#-------------------------------------------------------------------------------
#REMOVABLE_MOUNTPOINT=/mnt/cdrom



#-------------------------------------------------------------------------------
# Check if the directory for the log file exists.
#-------------------------------------------------------------------------------
if [ ! -d $LOGDIR ] ; then
   echo "=================================================================" > $LOG_ERROR_LOG
   echo -e "Restore Upgrade Data log for `date`." >> $LOG_ERROR_LOG
   echo "Restore upgrade data log directory, <$LOGDIR>, does not exist. Program exiting." >> $LOG_ERROR_LOG
   exit_cleanup 1
fi


#-------------------------------------------------------------------------------
# Start a new log to record save upgrade data actions.
#-------------------------------------------------------------------------------
echo "=================================================================" > $LOG
echo -e "Restore Upgrade Data log for `date`." >> $LOG
echo >> $LOG



#-------------------------------------------------------------------------------
#  Set the output media mount point variable and process accordingly
#   Initially only restore from hard disk, later possibly, restore from DVD
#-------------------------------------------------------------------------------
MEDIA_TYPE=0
if test "$MEDIA_TYPE" = "0" ; then

   MOUNTPOINT=$UPGRADE_MOUNTPOINT

   #----------------------------------------------------------------------------
   # Mount the media. Fixed disk in this case, /dev/hda2 possibly.
   #----------------------------------------------------------------------------
   mount -v $UPGRADE_MOUNTPOINT > $MOUNT_OUTPUT 2>&1
   if [ $? -ne 0 ] ; then

      #-------------------------------------------------------------------------
      # write protect errors?  This shouldn't happen if restore fom hard disk
      #-------------------------------------------------------------------------
      if grep "write-protected" $MOUNT_OUTPUT; then
         echo "The mountpoint, $UPGRADE_MOUNTPOINT, is write protected" >> $LOG
         exit_cleanup 2
      fi

      #-------------------------------------------------------------------------
      # already mounted?  Not an error, but a non-zero return code.  Continue
      #-------------------------------------------------------------------------
      if grep "already mounted" $MOUNT_OUTPUT; then
         echo "The mountpoint, $UPGRADE_MOUNTPOINT, is already mounted.  Continuing..." >> $LOG

      #-------------------------------------------------------------------------
      # other error?  Game over.
      #-------------------------------------------------------------------------
      else
         echo "The mountpoint, $UPGRADE_MOUNTPOINT, cannot be mounted.  Error is $?" >> $LOG
         exit_cleanup 4
      fi
      
   fi

   #----------------------------------------------------------------------------
   # good mount of output device, continue
   #----------------------------------------------------------------------------
   echo "Upgrade partition mounted at $UPGRADE_MOUNTPOINT." >> $LOG

   #----------------------------------------------------------------------------
   # Test for the existence of the "doRestore' indicator file.  If this file is
   # present, continue on with the restore process.
   #----------------------------------------------------------------------------
   if [ -e $INDICATOR_FILE ]; then
   
      SAVED_CLASSPATH=$CLASSPATH
      
if [ "${DEBUG_JARS_DIRECTORY}" != "" ] ; then
  if [ -d ${DEBUG_JARS_DIRECTORY} ] ; then
    for i in ${DEBUG_JARS_DIRECTORY}/*.jar
    do
       debug_jars=${debug_jars}:$i
    done
  fi
fi

      CLASSPATH=${DEBUG_JARS_DIRECTORY}:${debug_jars}:/usr/websm/codebase/pluginjars/hsc.jar:/usr/websm/codebase/wsm.jar:/usr/websm/codebase/pluginjars/sniacimom.jar
      
      #-------------------------------------------------------------------------
      # account for ALL svcagent jar files
      #-------------------------------------------------------------------------
      for f in /usr/svcagent/lib/*.jar
      do
         if [ -f $f ]; then
            CLASSPATH=$CLASSPATH:$f
         fi
      done
      CLASSPATH=$CLASSPATH:$SAVED_CLASSPATH
      
      JAVAPATH="/opt/IBMJava2-131/jre/bin/"
      x=`type -p java 2>/dev/null`
      if [ "$x" != "" ]
      then
        JAVAPATH=`/usr/bin/dirname $x`
      fi
      export CLASSPATH
      export PATH=$JAVAPATH:$PATH

      #-------------------------------------------------------------------------
      # Launch the Java program which iterates through the list of restore
      # scripts
      #-------------------------------------------------------------------------
      echo "Issuing command to restore the saved files" >> $LOG
      
      javaw com.ibm.hsc.websm.launch.hscmgt.RestoreUpgradeData
      RESTORE_RC=$?
      
      echo "Restore file processing completed with rc = $RESTORE_RC." >> $LOG

      CLASSPATH=$SAVED_CLASSPATH
      export CLASSPATH
      
      #-------------------------------------------------------------------------
      # Remove the old saveUpgradeLog file and the indicator file only if there
      # was a successful completion
      #-------------------------------------------------------------------------
      if [ $RESTORE_RC -eq 0 ]; then
      
         rm -f $LOGDIR/saveUpgradeData.log
         rm -f $INDICATOR_FILE
      
         #
         # Additional cleanup - there is the possibility that the directories under /home
         # (all the users' dirs) may have been re-generated via this restore process.
         # Unfortunately, if the directory itself was not part of the archive, it will
         # have been created with uid/gid of root/root as part of un-taring the actual
         # files in those directories. This is not good since the users will not be able
         # to access their home dirs! Change ownership of those subdirectories to the
         # corresponding userID.
         #
         for user in `echo /home/*`
         do
             chown ${user##/home/} $user
         done
      fi

   else
      echo "Indicator to restore saved upgrade data not found. No files restored." >> $LOG
      
   fi
   

   #----------------------------------------------------------------------------
   #  Completed the restore.  Unmount and exit.
   #----------------------------------------------------------------------------
   umount -v $UPGRADE_MOUNTPOINT > $MOUNT_OUTPUT 2>&1
   if [ $? -ne 0 ] ; then

      echo "Can't unmount the $UPGRADE_MOUNTPOINT volume, rc = $?." >> $LOG
      exit_cleanup 6

   else
      echo "$UPGRADE_MOUNTPOINT volume unmounted successfully." >> $LOG
      echo "Successful completion of restore of upgrade data." >> $LOG
      exit_cleanup 0
   fi

fi
